package com.yangrong.thread.chapter7;

/**
 * this锁
 * 
 * @author YangRong
 * @date 2020年5月27日
 */
public class SynchronizedThis {

	public static void main(String[] args) {
		ThisLock thisLock1 = new ThisLock();
		ThisLock thisLock2 = new ThisLock();

		new Thread("T1") {
			@Override
			public void run() {
				thisLock1.m1();
			}
		}.start();

		new Thread("T2") {
			@Override
			public void run() {
				thisLock2.m2();
			}
		}.start();
	}
}

class ThisLock {

	public synchronized void m1() {
		try {
			System.out.println(Thread.currentThread().getName());
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public synchronized void m2() {
		try {
			System.out.println(Thread.currentThread().getName());
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
